package org.taurusxi.apiGateway;

import com.netflix.zuul.FilterFileManager;
import com.netflix.zuul.FilterLoader;
import com.netflix.zuul.groovy.GroovyCompiler;
import com.netflix.zuul.groovy.GroovyFileFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.taurusxi.apiGateway.mvc.FilterConfiguaration;

@EnableZuulProxy
@EnableConfigurationProperties(value = {FilterConfiguaration.class})
@SpringBootApplication
public class ApiGatewayApplication {

	public static void main(String[] args) {
		SpringApplication.run(ApiGatewayApplication.class, args);
	}



	@Bean
	public FilterLoader filterLoader(FilterConfiguaration filterConfiguaration){
		FilterLoader filterLoader = FilterLoader.getInstance();
		filterLoader.setCompiler(new GroovyCompiler());
		try{
			FilterFileManager.setFilenameFilter(new GroovyFileFilter());
			FilterFileManager.init(filterConfiguaration.getInterval(),
					filterConfiguaration.getRoot()+"/pre",
					filterConfiguaration.getRoot()+"/post");
		}catch (Exception e){
			throw new RuntimeException(e);
		}

		return filterLoader;
	}
}
